import { ref } from 'vue'
import { defineStore } from 'pinia'

export const usePiniaStore = defineStore('pinia', {

  state: () => ({
    myLike: ref<Array<any>>([]),
    myCollection: ref<Array<any>>([]),
    myFootsteps: ref<Array<any>>([]),

  }),

  actions: {
    toggleMyLike(data: { id: number, src: string }) {
      let { id } = data
      if (!this.myLike.length) {
        this.myLike.push(data)
      } else {
        let index = this.myLike.findIndex((item) => item.id == id)
        if (index == -1) {
          this.myLike.push(data)
        } else {
          this.myLike.forEach((m, i) => {
            if (m.id === id) {
              this.myLike.splice(i, 1)
            }
          })
        }
      }

    },

    toggleMyCollection(data: { id: number, src: string }) {
      let { id } = data
      if (!this.myCollection.length) {
        this.myCollection.push(data)
      } else {
        let index = this.myCollection.findIndex((item) => item.id == id)
        if (index == -1) {
          this.myCollection.push(data)
        } else {
          this.myCollection.forEach((m, i) => {
            if (m.id === id) {
              this.myCollection.splice(i, 1)
            }
          })
        }
      }
    },

    toggleMyFootsteps(data: { id: number | undefined, src: string }) {
      console.log(this.myFootsteps);

      let { id } = data
      if (!this.myFootsteps.length) {
        this.myFootsteps.push(data)
      } else {
        let index = this.myFootsteps.findIndex((item) => item.id == id)
        if (index == -1) {
          this.myFootsteps.push(data)
        } else {
          this.myFootsteps.forEach((m, i) => {
            if (m.id === id) {
              this.myFootsteps.splice(i, 1)
            }
          })
        }
      }
    }
  },

  // 开启数据缓存
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'my_user',
        storage: localStorage,
      }
    ]
  }
})
